unit UCadModelo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Modelo, DB, DBTables, Grids, DBGrids, StdCtrls, Buttons,
  CheckLst, ExtCtrls, Mask, DBCtrls, UDptoUteis;

type
  TFCadModelo = class(TFModelo)
    qPadraoMODDESCRICAO: TStringField;
    qPadraoMODID: TIntegerField;
    lbl1: TLabel;
    dbedt1: TDBEdit;
    lbl2: TLabel;
    dbedt2: TDBEdit;
    dbgrdModeloEtapa: TDBGrid;
    Panel1: TPanel;
    dsModeloEtapa: TDataSource;
    QModeloEtapa: TQuery;
    QModeloEtapaMODETPDESCRICAO: TStringField;
    QModeloEtapaMODETPID: TIntegerField;
    QModeloEtapaMODID: TIntegerField;
    Edit1: TEdit;
    lbl3: TLabel;
    procedure dbgrdModeloEtapaKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure qPadraoAfterScroll(DataSet: TDataSet);
    procedure dbgrdModeloEtapaDblClick(Sender: TObject);
    procedure qPadraoAfterInsert(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FCadModelo: TFCadModelo;

implementation

uses UCadEtapaInfra, UCadModeloEtapa;

{$R *.dfm}

procedure TFCadModelo.dbgrdModeloEtapaKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
 var x,i:integer;
      FormularioFilho :TFCadModeloEtapa;
begin
  inherited;
   if Ord(Key) = 45 then
  Begin

      IF qPadrao.State = dsbrowse then
      begin
        FCadModeloEtapa := TFCadModeloEtapa.create(nil);
        //
        FormularioFilho := FCadModeloEtapa;

        FormularioFilho.qFilha.close;
        FormularioFilho.qFilha.open;
        FormularioFilho.qFilha.append;
        //
        for i:= 0 to qPadrao.FieldCount -1 do
        begin
          for x:= 0 to FormularioFilho.qFilha.FieldCount-1 do
          begin
            if qPadrao.Fields[i].FieldName = FormularioFilho.qFilha.Fields[x].FieldName then
             FormularioFilho.qFilha.FieldByName(FormularioFilho.qFilha.Fields[x].FieldName).AsVariant := qPadrao.fieldByname(qPadrao.Fields[i].FieldName).AsVariant;

          end;
        end;

        FormularioFilho.qFilha.FieldByName('MODETPID').AsInteger := DPTOUltimo('MODELOETAPA', 'MODETPID', 'WHERE MODID = ' + quotedStr(FormularioFilho.qFilha.FieldByName('MODID').ASSTRING));

        FormularioFilho.Showmodal;
        //
        QModeloEtapa.Close;
        QModeloEtapa.Open;
      end;
  end;
end;

procedure TFCadModelo.dbgrdModeloEtapaDblClick(Sender: TObject);
begin
  inherited;
      FCadModeloEtapa := TFCadModeloEtapa.create(nil);
      //
      FCadModeloEtapa.qFilha.close;
      FCadModeloEtapa.qFilha.sql.Clear;

      
      vSql := 'SELECT * FROM MODELOETAPA'
          + ' WHERE MODID = ' + quotedStr(QModeloEtapa.FieldByName('MODID').ASSTRING)
          + ' and MODETPID = '  + quotedStr(QModeloEtapa.FieldByName('MODETPID').ASSTRING);

      FCadModeloEtapa.qFilha.sql.Add(vSql);
      FCadModeloEtapa.qFilha.open;
      //
      FCadModeloEtapa.Showmodal;
      QModeloEtapa.Close;
      QModeloEtapa.Open;
end;


procedure TFCadModelo.qPadraoAfterScroll(DataSet: TDataSet);
begin
  inherited;
  if qPadrao.fieldByName('MODID').AsString <> '' then
  Begin
    QModeloEtapa.Close;
    QModeloEtapa.SQL.Clear;
    QModeloEtapa.SQL.Add(' SELECT * FROM MODELOETAPA WHERE MODID = ' + QuotedStr(qPadrao.fieldByName('MODID').AsString) );
    QModeloEtapa.Open
  end;

end;



procedure TFCadModelo.qPadraoAfterInsert(DataSet: TDataSet);
begin
  inherited;
  qPadrao.FieldByName('MODID').AsInteger := DPTOUltimo('MODELO', 'MODID','');
end;

end.
